package cn.kmsoft.spatialboot.service.executor;

import cn.kmsoft.stgeometry.constant.DBType;
import cn.kmsoft.stgeometry.util.GeoJsonUtil;
import cn.kmsoft.stgeometry.util.GeomUtil;
import cn.kmsoft.wololo.object.GeoJsonFeature;
import com.alibaba.fastjson.JSONObject;
import cn.kmsoft.spatialboot.config.dbswitch.DataSourceContextHolder;
import cn.kmsoft.spatialboot.common.object.TaskParam;
import cn.kmsoft.spatialboot.common.object.TaskResult;
import cn.kmsoft.spatialboot.utils.EllipsoidalAreaCalculatorEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;


/**
 * @author  wangq
 * @date 2019/7/17.
 */
//@Slf4j
public class IntersectExecutor extends BaseParallelExecutor {
    private static final Logger logger = LoggerFactory.getLogger(IntersectExecutor.class);
    
    private double centralMeridian=105.0;
    private String sourceEPSG="EPSG:4496"; //国家大地坐标系2000，6度分带，18度带
    private String targetEPSG="EPSG:4490";//国家大地坐标系2000 地理坐标系 无投影



    @Override
    protected TaskResult task(int index, TaskParam param, JSONObject inputFeature) {
        //设置sde的数据库实例
        String dbInstance = param.getInstance();
        DataSourceContextHolder.setDataSourceType(dbInstance);
        String dbTypeName = param.getDbType();
        DBType dbType = DBType.getDBTypeByName(dbTypeName);

        long begin = System.currentTimeMillis();
        List<GeoJsonFeature> features;
        int wkid = param.getWkid();
        try {
            features = GeoJsonUtil.parse(
                    param.getSpatialOperate().query(
                            param.getTableId(),
                            GeomUtil.geojsonToWkt(inputFeature.getJSONObject("geometry").toString()),
                            param.getWkid(),
                            param.getFgeo_wkid()
                    ),
                    param.getWkid(),
                    dbType);
        } finally {
            //清除该数据库的连接
            DataSourceContextHolder.clearDataSourceType();
        }

        logger.info("从数据库获取数据耗时：{}", (System.currentTimeMillis() - begin));
        if (param.isReturnEllipsoidalArea() == true) {
            //椭球面积计算
            EllipsoidalAreaCalculatorEx calculator = new EllipsoidalAreaCalculatorEx(centralMeridian, sourceEPSG, targetEPSG);
            features.forEach(feature ->
                    {
                        feature.getProperties().put(
                                "TQMJ",
                                calculator.getFeatureArea(
                                        GeomUtil.geojsonToWkt(JSONObject.toJSONString(feature.getGeometry())),
                                        param.getDigits(),
                                        wkid
                                )
                        );
                    }
            );
        } else {
            EllipsoidalAreaCalculatorEx calculator = new EllipsoidalAreaCalculatorEx(centralMeridian, sourceEPSG, targetEPSG);
            features.forEach(feature ->
                    {
                        feature.getProperties().put(
                                "PMMJ",
                                calculator.getFeatureArea2(
                                        GeomUtil.geojsonToWkt(JSONObject.toJSONString(feature.getGeometry())),
                                        param.getDigits()
                                )
                        );
                    }
            );
        }

        TaskResult result = new TaskResult();
        result.setFeatures(features);
        result.setIndex(index);
        return result;
    }
}
